<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use GTClient;
use GTPushRequest;
use GTPushMessage;
use GTNotification;
use GTSettings;
use GTStrategy;
use GTPushChannel;
use GTIos;
use GTAps;
use GTAlert;
use GTMultimedia;
use GTAndroid;
use GTUps;
use GTThirdNotification;


class GetuiController extends Controller
{

    // 个推（TODO 未测试）
    public function initialize()
    {
        parent::initialize();
    }


    // 推送API_根据cid进行单推
    public function pushToSingleByCid($c_id, string $msg = "设置通知内容", string $title = "设置通知标题")
    {
        //创建API，APPID等配置参考 环境要求 进行获取
        $api     = new GTClient("https://restapi.getui.com", "APPKEY", "APPID", "MASTERSECRET");
        $push    = new GTPushRequest();
        $message = new GTPushMessage();
        $notify  = new GTNotification();

        //设置推送参数
        //$push->setRequestId("请求唯一标识号");
        $push->setRequestId(md5(time().mt_rand(1, 9999999)));
        $notify->setTitle($title);
        $notify->setBody($msg);
        //点击通知后续动作，目前支持以下后续动作:
        //1、intent：打开应用内特定页面url：打开网页地址。2、payload：自定义消息内容启动应用。3、payload_custom：自定义消息内容不启动应用。4、startapp：打开应用首页。5、none：纯通知，无后续动作
        $notify->setClickType("none");

        $message->setNotification($notify);
        // TODO 可以不用设置
        //$push = $this->set($push, $title, $msg);
        $push->setPushMessage($message);
        $push->setCid($c_id);  //  //$userInfo里面只有一个cid是由前端生成的
        //处理返回结果
        $result = $api->pushApi()->pushToSingleByCid($push);

        return $result;
    }

    // 推送设置
    protected function set($push, $title, $msg)
    {
        //设置setting
        $set = new GTSettings();
        $set->setTtl(3600000);

        //      此处分为4个类型
        //      1: 表示该消息在用户在线时推送个推通道，用户离线时推送厂商通道;  我使用的是1
        //      2: 表示该消息只通过厂商通道策略下发，不考虑用户是否在线;
        //      3: 表示该消息只通过个推通道下发，不考虑用户是否在线；
        //      4: 表示该消息优先从厂商通道下发，若消息内容在厂商通道代发失败后会从个推通道下发。
        $strategy = new GTStrategy();
        $strategy->setDefault(GTStrategy::STRATEGY_GT_FIRST);
        $set->setStrategy($strategy);
        $push->setSettings($set);
        //厂商推送消息参数
        $pushChannel = new GTPushChannel();
        //ios
        $ios = new GTIos();
        $ios->setType("notify");
        $ios->setAutoBadge("1");
        $ios->setPayload("ios_payload");
        $ios->setApnsCollapseId("apnsCollapseId");
        //aps设置
        $aps   = new GTAps();
        $alert = new GTAlert();
        $alert->setTitle($title);
        $alert->setBody($msg);
        $aps->setAlert($alert);
        $ios->setAps($aps);

        $multimedia  = new GTMultimedia();
        $multimedia2 = new GTMultimedia();
        $multimedia->setUrl("url");
        $multimedia->setType(1);
        $multimedia->setOnlyWifi(false);
        $multimedia2->setUrl("url2");
        $multimedia2->setType(2);
        $multimedia2->setOnlyWifi(true);
        $ios->setMultimedia(array($multimedia));
        $ios->addMultimedia($multimedia2);
        $pushChannel->setIos($ios);
        //安卓
        $android           = new GTAndroid();
        $ups               = new GTUps();
        $thirdNotification = new GTThirdNotification();
        $thirdNotification->setTitle($title);
        $thirdNotification->setBody($msg);
        //      和上面的方法中setClickType()的类型对应，一定要按照类型去写参数。下面有图，详细参考文档
        //      消息通知的类型对应分别有
        //      CLICK_TYPE_INTENT = "intent";
        //      CLICK_TYPE_URL = "url";
        //      CLICK_TYPE_PAYLOAD = "payload";
        //      CLICK_TYPE_STAERAPP = "startapp";
        //      CLICK_TYPE_NONE = "none";
        $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_STAERAPP);
        $ups->setNotification($thirdNotification);
        $android->setUps($ups);
        $pushChannel->setAndroid($android);
        $push->setPushChannel($pushChannel);

        return $push;
    }

    // 统计API_获取单日推送数据
    public function queryPushResultByDate()
    {
        //创建API，APPID等配置参考 环境要求 进行获取
        $api = new GTClient("https://restapi.getui.com", "APPKEY", "APPID", "MASTERSECRET");
        //处理返回结果
        $result = $api->statisticsApi()->queryPushResultByDate("年-月-日");
    }

    // 用户API_查询用户状态
    public function queryUserStatus()
    {
        //创建API，APPID等配置参考 环境要求 进行获取
        $api   = new GTClient("https://restapi.getui.com", "APPKEY", "APPID", "MASTERSECRET");
        $array = array(CID1);
        //处理返回结果
        $result = $api->userApi()->queryUserStatus($array);
    }
}


